package com.xyhy.core.utils;

import java.util.Collection;
import java.util.Objects;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.BooleanUtils;
import com.xyhy.core.base.exception.ServiceException;

public class Asserts {
  public static void isTrue(boolean expression, String message, Object... arguments) {
    isTrueToast(expression, message, null, arguments);
  }

  public static void isTrueToast(boolean expression, String message, Throwable throwable, Object... arguments) {
    if (!expression) {
      throw new ServiceException(StrUtil.format(message, arguments), throwable);
    }
  }

  public static void isFalse(boolean expression, String message, Object... arguments) {
    isTrueToast(!expression, message, null, arguments);
  }

  public static void isFalseToast(boolean expression, String message, Throwable throwable, Object... arguments) {
    isTrueToast(!expression, message, throwable, arguments);
  }

  public static void isNotTrue(Boolean expression, String message, Object... arguments) {
    isNotTrueToast(expression, message, null, arguments);
  }

  public static void isNotTrueToast(Boolean expression, String message, Throwable throwable, Object... arguments) {
    isTrueToast(BooleanUtils.isNotTrue(expression), message, throwable, arguments);
  }

  public static void isNull(Object object, String message, Object... arguments) {
    isNullToast(object, message, null, arguments);
  }

  public static void isNullToast(Object object, String message, Throwable throwable, Object... arguments) {
    if (Objects.nonNull(object)) {
      throw new ServiceException(StrUtil.format(message, arguments), throwable);
    }
  }

  public static void notNull(Object object, String message, Object... arguments) {
    notNullToast(object, message, null, arguments);
  }

  public static void notNullToast(Object object, String message, Throwable throwable, Object... arguments) {
    if (Objects.isNull(object)) {
      throw new ServiceException(StrUtil.format(message, arguments), throwable);
    }
  }

  public static void isBlank(String str, String message, Object... arguments) {
    isBlankToast(str, message, null, arguments);
  }

  public static void isBlankToast(String str, String message, Throwable throwable, Object... arguments) {
    if (StrUtil.isNotBlank(str)) {
      throw new ServiceException(StrUtil.format(message, arguments), throwable);
    }
  }

  public static void isNotBlank(String str, String message, Object... arguments) {
    isNotBlankToast(str, message, null, arguments);
  }

  public static void isNotBlankToast(String str, String message, Throwable throwable, Object... arguments) {
    if (StrUtil.isBlank(str)) {
      throw new ServiceException(StrUtil.format(message, arguments), throwable);
    }
  }

  public static void isEmpty(Collection<?> collection, String message, Object... arguments) {
    isEmptyToast(collection, message, null, arguments);
  }

  public static void isEmptyToast(Collection<?> collection, String message, Throwable throwable, Object... arguments) {
    if (CollUtil.isNotEmpty(collection)) {
      throw new ServiceException(StrUtil.format(message, arguments), throwable);
    }
  }

  public static void isNotEmpty(Collection<?> collection, String message, Object... arguments) {
    isNotEmptyToast(collection, message, null, arguments);
  }

  public static void isNotEmptyToast(Collection<?> collection, String message, Throwable throwable,
                                     Object... arguments) {
    if (CollUtil.isEmpty(collection)) {
      throw new ServiceException(StrUtil.format(message, arguments), throwable);
    }
  }

  public static void allNotNull(Collection<?> collection, String message, Object... arguments) {
    allNotNullToast(collection, message, null, arguments);
  }

  public static void allNotNullToast(Collection<?> collection, String message, Throwable throwable,
                                     Object... arguments) {
    if (CollUtil.hasNull(collection)) {
      throw new ServiceException(StrUtil.format(message, arguments), throwable);
    }
  }

}
